package gui;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Label;
import java.awt.Button;
import java.awt.TextField;
import java.awt.TextArea;

import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;

import daten.Diplomarbeit;
import daten.DiplomarbeitenContainer;
import daten.Professor;

public class DiplomarbeitErfassen extends Dialog implements ActionListener
	{
		private DiplomarbeitenContainer container;
		private Diplomarbeit subject;

		private TextField thema;
		private TextArea beschreibung;

		public DiplomarbeitErfassen(Frame owner)
		{
			super(owner, "Diplomarbeit erfassen", true);

			container = DiplomarbeitenContainer.instance();

			setLayout(new GridLayout(0, 1));

			Panel p1 = new Panel();
			p1.add(new Label("Thema: "));
			thema = new TextField(20);
			p1.add(thema);
			add(p1);

			Panel p2 = new Panel();
			p2.add(new Label("Beschreibung: "));
			beschreibung = new TextArea(20,100);
			p2.add(beschreibung);
			add(p2);

			Panel p = new Panel();
			Button abbrechen = new Button("Abbrechen");
			abbrechen.addActionListener(this);
			p.add(abbrechen);

			Button ok = new Button("Erfassen");
			ok.addActionListener(this);
			p.add(ok);
			
			Button uebernehmen = new Button("Uebernehmen");
			uebernehmen.addActionListener(this);
			p.add(uebernehmen);
			add(p);

			pack();
			setLocation(owner.getLocation().x + 10, owner.getLocation().y + 10);
			setVisible(true);
		}

		private void save()
		{
			subject.setThema(thema.getText());
			subject.setBeschreibung(beschreibung.getText());
		}

		private void onErfassen()
		{
		      if (checkData())
		      {    
		    	subject = new Diplomarbeit();
		    	save();
		    	if(container.addDiplomarbeit(subject))
		    		dispose();
		      }
		}
		
		private void onAbbrechen()
		{
			dispose();
		}
		
		private void onUebernehmen()
		{
		      if (checkData())
		      {    
		    	subject = new Diplomarbeit();
		    	save();
		    	if(container.addDiplomarbeit(subject))
		    		clearData();
		      }
		}

		private void clearData()
		{
			thema.setText("");
			beschreibung.setText("");
		}

		private boolean checkData()
		{
			return (!thema.getText().equals(""));
		}

		public void actionPerformed(ActionEvent e)
		{
			if (e.getActionCommand().equals("Abbrechen"))
				onAbbrechen();

			if (e.getActionCommand().equals("Erfassen"))
				onErfassen();

			if (e.getActionCommand().equals("Uebernehmen"))
				onUebernehmen();				
		}
}
